//洗涤统计 混合通用业务
import API from "./api";
import { formatNetTime } from "@/libs/tools";

let mixin = {
  mounted() {
    this.resetInit();
  },

  activated() {
    let userInfo = this.$store.state.user.userInfo.data;
    this.access = this.$store.state.user.access;
    if (this.access !== "wash") {
      this.getWashlList();
    }
  },

  methods: {
    resetInit() {
      let userInfo = this.$store.state.user.userInfo.data;
      this.access = this.$store.state.user.access;
      if (this.access == "wash") {
        this.formSearch.depaid = userInfo.depaid;
        this.handleSeacrh();
      } else {
        this.getWashlList();
      }
    },

    getWashlList() {
      API.getWashlList({ type: 1 })
        .then(res => {
          if (res.code == 200) {
            this.washList = res.data;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    formatTime: function(timeStr1, bool) {
      return formatNetTime(timeStr1, bool);
    },

    onStartTimeChange(startTime) {
      this.endTimeOption = {
        disabledDate(endTime) {
          return endTime < new Date(startTime);
        }
      };
    },

    onEndTimeChange(endTime) {
      this.startTimeOption = {
        disabledDate(startTime) {
          return startTime > new Date(endTime);
        }
      };
    },

    handleSeacrh() {
      let obj = _.clone(this.formSearch);
      if (obj.depaid == "") {
        this.$Message.error("查询洗涤公司必选！");
        return;
      }

      if (obj.startTime) {
        if (obj.endTime == "") {
          this.$Message.error("结束时间需要完善！");
          return;
        }
      }

      if (obj.endTime) {
        if (obj.startTime == "") {
          this.$Message.error("起始时间需要完善！");
          return;
        }
      }

      obj.pageNum = 1;
      this.formSearch = _.clone(obj);
      if (obj.startTime != "" || obj.endTime != "") {
        obj.startTime = this.UTC(obj.startTime, "starttime");
        obj.endTime = this.UTC(obj.endTime, "endtime");
      }
      this.getList(obj);
    },

    changePage() {
      let obj = _.clone(this.formSearch),
        maxPageNum = Math.ceil(obj.page.total / obj.page.pageSize);
      if (obj.pageNum + 1 <= maxPageNum) {
        obj.pageNum = maxPageNum;
      }
      this.formSearch = _.clone(obj);
      if (obj.startTime != "" || obj.endTime != "") {
        obj.startTime = this.UTC(obj.startTime, "starttime");
        obj.endTime = this.UTC(obj.endTime, "endtime");
      }
      this.getList(obj);
    },

    changePageA() {
      let obj = this.formSearchA,
        maxPageNum = Math.ceil(obj.pageA.total / obj.pageA.pageSize);
      if (obj.pageNum + 1 <= maxPageNum) {
        obj.pageNum = maxPageNum;
      }
      this.formSearchA = obj;
      this.getOne(obj);
    },

    getList(obj) {
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            this.page.total = data.total;
            this.page.pageSize = data.pageSize;
            this.data = data.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    getOne(obj) {
      API.getOne(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            this.pageA.total = data.total;
            this.pageA.pageSize = data.pageSize;
            this.dataA = data.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    goBack() {
      this.isShow = true;
      this.pageA.total = 0;
      this.pageA.pageSize = 20;
      this.dataA = [];
    },

    show(index) {
      this.currentData = this.data[index];
      this.isShow = false;
      this.formSeachA.billid = this.currentData.billid;
      this.getOne(this.formSeachA);
    }
  }
};

export default mixin;
